package com.killer.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.killer.db.service.impl.BaseServiceImpl;
import com.killer.goods.dto.SkuChangeDTO;
import com.killer.goods.entity.Sku;
import com.killer.goods.mapper.SkuMapper;
import com.killer.goods.service.ISkuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标准产品单位表 服务实现类
 * </p>
 *
 * @author killer
 * @since 2021-08-29
 */
@Service
public class SkuServiceImpl extends BaseServiceImpl<SkuMapper, Sku> implements ISkuService {

    @Override
    protected LambdaQueryWrapper<Sku> createQueryWrapper(Map<String, Object> searchMap) {
        LambdaQueryWrapper<Sku> queryWrapper = Wrappers.lambdaQuery(Sku.class);
        if (searchMap != null) {
            queryWrapper.eq(searchMap.get("spuId") != null, Sku::getSpuId, searchMap.get("spuId"));
        }
        return queryWrapper;
    }

    @Override
    public void changeSkuInventoryAndSaleNum(List<SkuChangeDTO> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return;
        }
        for (SkuChangeDTO dto : dtos) {
            int count=baseMapper.changeInventoryAndSaleNum(dto.getId(), dto.getNum());
            if (count <= 0) {
                throw new RuntimeException("库存不足");
            }
        }
    }
}
